﻿using System;
using System.Net;
using Google.Documents;
using Google.GData.Client;
using Google.GData.Documents;

namespace my_google_doc_online
{
    public class MyMethod
    {
        private GDataCredentials credentials;
        public bool loggedIn;
        public DocumentsRequest request;
        public DocumentsService service;

        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public void Login(string username, string password)
        {
            try
            {
                //将用户名密码类型转换为API所使用GDataCredentials类格式
                credentials = new GDataCredentials(username, password);
                //初始化请求对象设置
                RequestSettings settings = new RequestSettings("my_google_doc_online", credentials);
                //settings.AutoPaging = true;
                //settings.PageSize = 100;
                //请求Google Data API feed设置对象
                request = new DocumentsRequest(settings);
                //获取该服务对象
                service = request.Service;
                //new DocumentsService("my_google_doc_online");
                ((GDataRequestFactory)service.RequestFactory).KeepAlive = false;
                //WebProxy proxy = new WebProxy("127.0.0.1", 8087);
                //proxy.Credentials = CredentialCache.DefaultCredentials;
                //proxy.UseDefaultCredentials = true;
                //requestFactory.Proxy = proxy;
                //requestFactory.KeepAlive = false;
                //string t = service.QueryClientLoginToken();
                //service.setUserCredentials(username, password);
                //service.QueryAuthenticationToken();
                DocumentsListQuery query = new DocumentsListQuery();
                //尝试取回服务端一条文档列表数据
                //query.NumberToRetrieve = 1;
                DocumentsFeed qu = service.Query(query);
                loggedIn = true;
            }
            catch (CaptchaRequiredException e)
            {
                string u = e.Url;
                GDataGAuthRequestFactory requestFactory = (GDataGAuthRequestFactory)service.RequestFactory;
                requestFactory.CaptchaToken = e.Token;
            }
            catch (AuthenticationException e)
            {
                loggedIn = false;
                service = null;
                throw e;
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="host"></param>
        /// <param name="port"></param>
        public void Login(string username, string password, string host, int port)
        {
            try
            {
                credentials = new GDataCredentials(username, password);

                var settings = new RequestSettings("my_google_doc_online", credentials);
                settings.AutoPaging = true;
                settings.PageSize = 100;
                request = new DocumentsRequest(settings);
                service = request.Service;
                var requestFactory = (GDataRequestFactory)service.RequestFactory;
                //根据输入参数，创建WebProxy对象
                var proxy = new WebProxy(host, port);
                //proxy.Credentials = CredentialCache.DefaultCredentials;
                proxy.UseDefaultCredentials = true;
                //将proxy附到requestFactory对象的Proxy属性
                requestFactory.Proxy = proxy;
                requestFactory.KeepAlive = false;
                //service.setUserCredentials(username, password);
                DocumentsListQuery query = new DocumentsListQuery();
                query.NumberToRetrieve = 1;
                service.Query(query);
                loggedIn = true;
            }
            catch (AuthenticationException e)
            {
                loggedIn = false;
                service = null;
                throw e;
            }
        }

        public void Logout()
        {
            loggedIn = false;
            request.Service = null;
        }

        //public DocumentsFeed GetDocs()
        //{
        //    DocumentsListQuery query = new DocumentsListQuery();
        //    DocumentsFeed feed = service.Query(query);
        //    return feed;
        //}
    }
}